package hu.rsc.forbiddenwords.client.gui.language;

import hu.rsc.forbiddenwords.client.Forbiddenwords;
import hu.rsc.forbiddenwords.client.util.SimpleCallback;
import hu.rsc.forbiddenwords.shared.LanguageDTO;

import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HasVerticalAlignment;

public class LanguagePanel extends Composite {
    private LanguageTable table;
    private LanguageForm form;

    public LanguagePanel() {
	FlexTable main = new FlexTable();
	this.table = new LanguageTable(this);
	main.setWidget(0, 0, this.form = new LanguageForm(this));
	main.getFlexCellFormatter().setVerticalAlignment(0, 0, HasVerticalAlignment.ALIGN_TOP);
	main.setWidget(0, 1, this.table);
	initWidget(main);
    }

    public LanguageDTO getEntry() {
	return this.form.getEntry();
    }

    public Long getEntryId() {
	return getEntry().getId();
    }

    public void refreshFeedsTable() {
	this.table.refreshTable();
    }

    public void refreshForm(LanguageDTO entry) {
	this.form.refresh(entry);
    }

    public void save(LanguageDTO entry) {
	Forbiddenwords.getService().saveLanguage(entry, new SimpleCallback<LanguageDTO>() {

	    @Override
	    public void onSuccess(LanguageDTO result) {
		/*if (result != null) {
		    Feeds.feeds.put(result.getId(), result);
		}*/
		LanguagePanel.this.table.refreshTable();
		LanguagePanel.this.form.refresh(result);
	    }
	});
    }

}
